﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace syc.Jidong
{
    public partial class 设备附件 : System.Web.UI.Page
    {
        private void ShowMessage(string msg)
        {
            this.Panel_Msg.Visible = true;
            this.Message.Text = "·" + msg;
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = 0;
            this.Panel_Msg.Visible = false;
            if (!this.IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    int.TryParse(Request.QueryString["ID"], out id);
                }
                if (id > 0)
                {
                    this.BillID.Text = id.ToString();
                    this.SqlDataSource1.SelectCommand =
@"SELECT isnull(名称,'')
+'|=*'+isnull(型号,'')
+'|=*'+isnull(单位,'')
+'|=*'+cast(isnull(数量,-1) as varchar)
+'|=*'+cast(isnull( 单价,-1) as varchar)
+'|=*'+isnull(备注,'') as val
,'' as txt
FROM 设备附件 where 台账ID="+id;
                }
            }
        }

        protected void Button_Save_Click(object sender, EventArgs e)
        {
            string[] cell1 = this.Cell1.Value.Split(new string[] { "|=*" }, StringSplitOptions.None);
            string[] cell2 = this.Cell2.Value.Split(new string[] { "|=*" }, StringSplitOptions.None);
            string[] cell3 = this.Cell3.Value.Split(new string[] { "|=*" }, StringSplitOptions.None);
            string[] cell4 = this.Cell4.Value.Split(new string[] { "|=*" }, StringSplitOptions.None);
            string[] cell5 = this.Cell5.Value.Split(new string[] { "|=*" }, StringSplitOptions.None);
            string[] cell6 = this.Cell6.Value.Split(new string[] { "|=*" }, StringSplitOptions.None);
            
            this.RowList.Items.Clear();
            this.SqlDataSource1.DeleteParameters["ID"].DefaultValue = this.BillID.Text;
            this.SqlDataSource1.Delete();
            this.SqlDataSource1.InsertParameters["ID"].DefaultValue = this.BillID.Text;
            for (int i = 1; i < cell1.Length; i++)
            {
                ListItem li = new ListItem();
                li.Text=cell1[i]+"|=*"+cell2[i]+"|=*"+cell3[i]+"|=*"+cell4[i]+"|=*"+cell5[i]+"|=*"+cell6[i];
                li.Value="";
                this.RowList.Items.Add(li);
                this.SqlDataSource1.InsertParameters["名称"].DefaultValue = cell1[i];
                this.SqlDataSource1.InsertParameters["型号"].DefaultValue = cell2[i];
                this.SqlDataSource1.InsertParameters["单位"].DefaultValue = cell3[i];
                this.SqlDataSource1.InsertParameters["单价"].DefaultValue = cell5[i];
                this.SqlDataSource1.InsertParameters["数量"].DefaultValue = cell4[i];
                this.SqlDataSource1.InsertParameters["备注"].DefaultValue = cell6[i];
                this.SqlDataSource1.Insert();
            }
            this.SqlDataSource1.UpdateCommand = "update 设备台账 set 附件数=" + (cell1.Length-1) + " where ID=" + this.BillID.Text;
            this.SqlDataSource1.Update();
            this.ShowMessage("保存成功");
        }
    }
}
